package com.atguigu.demo.customer.controller;

import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {

    @Autowired  //自动装配 服务器启动检查所有需要装配的接口，根据实现类把内存中的对象装配上
    @Qualifier("li4")
    CustomerService customerService ;

    //@RequestMapping("/customer")
    @GetMapping("/customer")
    public List<Customer> getCustomer(@RequestParam("name") String name ,@RequestParam(value = "age",required = false,defaultValue = "0") Integer age){
        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("name", name).eq("age", age));

        return customerList;
    }

    @GetMapping("/customer/{id}.html")
    public Customer getCustomer( @PathVariable("id") String id  ){
        Customer customer = customerService.getById(id);
         int a= 1/0;
        return customer;
    }

    @PostMapping("/customer")
    public String saveCustomer(@RequestBody  Customer customer){
        //1216
        customer.setName("zhang1216");
        customerService.saveOrUpdate(customer);

        //1118
        customer.setName("zhang1118");
         customerService.saveCustomer(customer);



         return  "success";
    }


}
